package be.kuleuven.thesis.widget;

import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;
import be.kuleuven.thesis.R;
import be.kuleuven.thesis.framework.Song;
import be.kuleuven.thesis.status.Player;
import be.kuleuven.thesis.status.PlaylistGenerator;

public class PlaylistAdapter extends ArrayAdapter<Song>{
	private static final String TAG = "PlaylistAdpt";
	private static final int textViewResourceId = R.layout.rij_playlist;
	private Resources res;
	
	public PlaylistAdapter(Context context, List<Song> objects, Resources res) {
		super(context, textViewResourceId, objects);
		this.res = res;
	}
	
	@Override
	public View getView(int position, View view, ViewGroup parent) {
		ViewHolder holder;
		if (view == null ){//|| this.getItemViewType(position) != textViewResourceId){
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = vi.inflate(textViewResourceId, null);
			//Log.d(TAG, "inflated songs view");
			
			holder = new ViewHolder();
			holder.title = (TextView) view.findViewById(R.id.title_playlist);
            holder.artist = (TextView) view.findViewById(R.id.artist_playlist);
            holder.icon = (ImageView) view.findViewById(R.id.icon_playlist);
            holder.playIconLayout = (RelativeLayout) view.findViewById(R.id.play_icon_layout);
            holder.thumbsUp = (ToggleButton) view.findViewById(R.id.thumbs_up_playlist);
            holder.thumbsDown = (Button) view.findViewById(R.id.thumbs_down_playlist);
            holder.songInfo = (LinearLayout) view.findViewById(R.id.song_info_playlist);

            view.setTag(holder);
		}
		else {
			holder = (ViewHolder) view.getTag();
		}
	        
        Song song = this.getItem(position);
        if (song != null) {
        	
            holder.title.setText(song.getTitle());
            Player player = Player.getInstance();
            PlaylistGenerator generator = PlaylistGenerator.getInstance();
            
            if (holder.artist == null)
            	Log.e(TAG, "textview author not found");
            holder.artist.setText(song.getArtist());
            
            if(holder.icon != null && song.getImage() != -1) {
            	holder.icon.setImageDrawable(res.getDrawable(song.getImage()));
            	//dm.fetchDrawableOnThread(u.getImage(), icon);
            }
            
            if(position != player.getCurrentIndex()-1) {
            	holder.playIconLayout.setVisibility(RelativeLayout.GONE);
            } else {
            	holder.playIconLayout.setVisibility(RelativeLayout.VISIBLE);
            }
            if(generator.isRadioOn()) {
            	holder.thumbsUp.setVisibility(ToggleButton.VISIBLE);
            	if(generator.isLiked(song))
            		holder.thumbsUp.setChecked(true);
            	else holder.thumbsUp.setChecked(false);
            } else {
            	holder.thumbsUp.setVisibility(ToggleButton.GONE);
            }
            
            holder.songInfo.setFocusable(true);
            holder.songInfo.setClickable(true);
            holder.songInfo.setTag(position);
            holder.songInfo.setOnClickListener(new View.OnClickListener() {
            	@Override
            	public void onClick(View view) {
            		int position = (Integer) view.getTag();
            		Player.getInstance().playSongInPlaylist(position+1);
            		notifyDataSetChanged();
            	}
            });
            
            holder.thumbsUp.setTag(song);
            holder.thumbsUp.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					Song song = (Song) view.getTag();
					PlaylistGenerator generator = PlaylistGenerator.getInstance();
					if(generator.isLiked(song))
						generator.unlikeSong(song);
					else generator.likeSong(song);
				}
			});
            
            holder.thumbsDown.setTag(position);
            holder.thumbsDown.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View view) {
					int position = (Integer) view.getTag();
					PlaylistGenerator generator = PlaylistGenerator.getInstance();
					if(generator.isRadioOn()) {
						Song song = getItem(position);
						generator.dislikeSong(song);
					}
					Player.getInstance().removeSong(position+1);
				}
			});
        } else
        	Log.e(TAG, "Song on position " + position + "not found!");
        return view;
	}
	
	static class ViewHolder {
		LinearLayout songInfo;
		TextView title;
        TextView artist;
        ImageView icon;
        RelativeLayout playIconLayout;
        ToggleButton thumbsUp;
        Button thumbsDown;
    }
}
